package net.uhuijia.user.userCart.service.impl;

import net.uhuijia.constants.DBConst;
import net.uhuijia.dao.param.UserCartParam;
import net.uhuijia.enums.ErrorCodeEnum;
import net.uhuijia.exception.BusinessException;
import net.uhuijia.goods.goodsku.persistence.model.GoodSku;
import net.uhuijia.goods.goodspu.clientservice.GoodSpuClientService;
import net.uhuijia.goods.goodspu.persistence.model.GoodSpu;
import net.uhuijia.shop.info.persistence.model.Shop;
import net.uhuijia.shop.info.service.ShopService;
import net.uhuijia.user.userCart.service.UserCartService;
import net.uhuijia.user.userCartItem.persistence.model.UserCartItem;
import net.uhuijia.user.userCartItem.service.UserCartItemService;
import net.uhuijia.user.usermall.persistence.model.UserMall;
import net.uhuijia.vo.cart.UserCartItemVo;
import net.uhuijia.vo.cart.UserCartVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import net.uhuijia.appbase.service.CrudService;
import net.uhuijia.user.userCart.persistence.dao.UserCartDao;
import net.uhuijia.user.userCart.persistence.model.UserCart;
import net.uhuijia.user.userCart.persistence.model.UserCartExample;
import net.uhuijia.user.userCart.persistence.model.UserCartExample.Criteria;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class UserCartServiceImpl
     extends CrudService<UserCartDao,UserCart,UserCartExample> implements UserCartService {
    @Autowired
    private UserCartDao userCartDao;
    @Autowired
    private GoodSpuClientService goodSpuClientService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private UserCartItemService userCartItemService;

    @Override
    public UserCartItem addCart(UserMall userMall ,String spuId, Integer count, String propertyId) {
        if(!StringUtils.isNoneBlank(spuId,propertyId) || count ==null)
            throw  new IllegalArgumentException();
        //检测该商品是否下架
        GoodSpu goodSpu = this.goodSpuClientService.getGoodSpuBySpuId(spuId);
        if(goodSpu!=null){
            GoodSku goodSku = this.goodSpuClientService.getGoodSkuBySpuIdAndPropertyId(spuId,propertyId);
            if(goodSku!=null){
                //效验商品库存是否足够
                boolean isEnough =  this.goodSpuClientService.checkGoodSkuInvertoryIsEnough(spuId,propertyId,count);
                if(isEnough){
                    //根据商品查询店铺Id
                    String shopId = goodSpu.getShopId();
                    Shop shop = this.shopService.get(shopId);
                    if(shop!=null){
                        //判断用户是否添加过该店铺的商品
                        UserCart userCart = this.getUserCartByUserIdAndShopId(userMall.getId(),shopId);
                        if(userCart==null){
                            userCart = this.addUserCart(userMall.getId(),shopId);
                        }
                        //判断用户之前是否添加过该商品
                        UserCartItem userCartItem = this.userCartItemService.getUserCartItemByUserIdAndCartIdAndSkuId(userMall.getId(),userCart.getId(),goodSku.getId());
                        if(userCartItem !=null){
                            //修改购物车数量
                            int oldCount = userCartItem.getCount()==null?0:userCartItem.getCount().intValue();
                            count = oldCount + count;
                            userCartItem.setCount(count);
                            this.userCartItemService.edit(userCartItem);
                        }else{
                            //添加购物车商品
                            userCartItem = this.userCartItemService.addUserCartItem(userMall.getId(),userCart.getId(),spuId,goodSku.getId(),count);
                        }
                        return userCartItem;
                    }else{
                        throw  new BusinessException(ErrorCodeEnum.shop_not_exist_error);
                    }
                }else{
                    throw  new BusinessException(ErrorCodeEnum.sku_invertory_not_enough_error);
                }
            }else{
                throw  new BusinessException(ErrorCodeEnum.good_already_down_error);
            }
        }else{
            throw  new BusinessException(ErrorCodeEnum.good_already_down_error);
        }
    }
    /**
     * @Author xzj
     * @Description //根据用户id和店铺Id查询UserCart
     * @Date  2018/10/8
     * @Param
     * @return
     **/
    @Override
    public UserCart getUserCartByUserIdAndShopId(String userId, String shopId) {
        if(!StringUtils.isNoneBlank(userId,shopId))
            throw new IllegalArgumentException();
        UserCart userCart = this.userCartDao.getUserCartByUserIdAndShopId(new UserCartParam(userId,shopId));
        return userCart;
    }

    /**
     * @Author xzj
     * @Description //添加购物车
     * @Date  2018/10/8
     * @Param
     * @return
     **/
    public UserCart addUserCart(String userId,String shopId){
        UserCart userCart = new UserCart();
        userCart.setUserId(userId);
        userCart.setShopId(shopId);
        this.add(userCart);
        return userCart;
    }
    /**
     * @Author xzj
     * @Description //查询用户购物车数量
     * @Date  2018/10/8
     * @Param
     * @return
     **/
    @Override
    public int findUserCartCountByUserId(String userId) {
        UserCartExample example = new UserCartExample();
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andUserIdEqualTo(userId);
        Integer count = this.count(example);
        return count==null?0:count.intValue();
    }

    /**
     * @Author xzj
     * @Description //分页查询购物车店铺
     * @Date  2018/10/8
     * @Param
     * @return
     **/
    @Override
    public List<UserCart> findUserCartListByPageAndUserId(UserMall userMall, UserCartExample example) {
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andUserIdEqualTo(userMall.getId());
        List<UserCart> userCarts = this.list(example);
        return userCarts;
    }

    @Override
    public List<UserCartVo> findCartListByUserIdAndPage(UserMall userMall, UserCartExample example) {
        List<UserCart> userCarts = this.findUserCartListByPageAndUserId(userMall,example);
        List<UserCartVo> userCartVos = new ArrayList<>();
        for(UserCart userCart:userCarts){
            Shop shop = this.shopService.get(userCart.getShopId());
            if(shop!=null){
                UserCartVo userCartVo = new UserCartVo();
                userCartVo.setCartId(userCart.getId());
                userCartVo.setShopId(userCart.getShopId());
                userCartVo.setShopName(shop.getShopName());
                // 获取店铺购物车下的商品信息
                List<UserCartItemVo> userCartItemVos = this.userCartItemService.findUserCartItemVoByCartId(userCart.getId());
                userCartVo.setUserCartItems(userCartItemVos);
                userCartVos.add(userCartVo);
            }else
                continue;
        }
        return userCartVos;
    }
}